﻿using System;
using System.Threading;
using DuplicateFileFinder.Model;

namespace DuplicateFileFinder.Infrastructure
{
    public class UIManager
    {
        private SynchronizationContext _synchContext;
        private static IMainForm _form;

        public static UIManager Instance { get; private set; }

        private UIManager()
        {
            _synchContext = SynchronizationContext.Current;
        }

        public static void Initialize(IMainForm form)
        {
            _form = form;
            Instance = new UIManager();
        }

        public void ClearTreeView()
        {
            _synchContext.Send(new SendOrPostCallback(privateClearTV), null);
        }

        private void privateClearTV(object obj)
        {
            _form.ClearTreeView(obj);
        }

        public void UpdateFilesCount(long count)
        {
            _synchContext.Send(new SendOrPostCallback(privateUpdateFilesCount), count);
        }

        public void UpdateDuplicatesCount(long count)
        {
            _synchContext.Send(new SendOrPostCallback(privateUpdateDuplicatesCount), count);
        }

        private void privateUpdateDuplicatesCount(object count)
        {
            _form.UpdateDuplicatesFoundCount((long)count);
        }

        private void privateUpdateFilesCount(object count)
        {
            _form.UpdateFilesProcessedCount((long)count);
        }

        public void UpdateStatus(string msg)
        {
            _synchContext.Send(new SendOrPostCallback(privateUpdateStatus), msg);
        }

        private void privateUpdateStatus(object msg)
        {
            _form.UpdateStatus(msg as String);
        }

        public void AddFile(FileProperty fInfo)
        {
            _synchContext.Send(new SendOrPostCallback(addFile), fInfo);
        }

        private void addFile(Object fInfo)
        {
            var fileInfo = fInfo as FileProperty;

            var key = fileInfo.Hash;
            var file = fileInfo.Path;

            _form.AddFile(key, file);
        }

    }
}
